//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------
using System;
using CommunityServer.Components;

namespace CommunityServer.SqlDataProvider
{
	/// <summary>
	/// Summary description for SQLHelper.
	/// </summary>
	public class SQLHelper
	{
		private SQLHelper(){}

		#region Public Helpers

		/// <summary>
		/// Convert dates to a dictionary sortable string
		/// </summary>
		/// <param name="date"></param>
		/// <returns></returns>
		public static string GetSafeSqlDateTimeFormat(DateTime date)
		{
			return date.ToString(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.SortableDateTimePattern);
		}

		/// <summary>
		/// Auto range DateTime to SQL safe values
		/// </summary>
		/// <param name="date"></param>
		/// <returns></returns>
		public static DateTime GetSafeSqlDateTime (DateTime date) 
		{

			if (date < DateTime.MinValue)
				return (DateTime) System.Data.SqlTypes.SqlDateTime.MinValue;
			else if (date > DateTime.MaxValue)
				return (DateTime) System.Data.SqlTypes.SqlDateTime.MaxValue;
			return date;

		}

		/// <summary>
		/// Auto range integers to SQL safe values
		/// </summary>
		/// <param name="i"></param>
		/// <returns></returns>
		public static int GetSafeSqlInt (int i) 
		{

			if (i <= (int)System.Data.SqlTypes.SqlInt32.MinValue)
				return (int) System.Data.SqlTypes.SqlInt32.MinValue + 1;
			else if (i >= (int)System.Data.SqlTypes.SqlInt32.MaxValue)
				return (int) System.Data.SqlTypes.SqlInt32.MaxValue -1;
			return i;

		}

        public static object StringOrNull(string text)
        {
            if(Globals.IsNullorEmpty(text))
                return DBNull.Value;

            return text;
        }

		#endregion
	}
}
